package com.inspur.dgov.vlbod.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inspur.dgov.vlbod.entity.DemandBroadcast;
import com.inspur.dgov.vlbod.entity.DemandVideoUrl;
import com.inspur.dgov.vlbod.http.DgovResponse;
import com.inspur.dgov.vlbod.service.DemandBroadcastService;
import com.inspur.dgov.vlbod.service.UserRecordService;
import com.inspur.dgov.vlbod.vo.DemandBroadcastVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 点播服务控制器
 *
 * @Date 2023/5/28 18:42
 * @Version 1.0
 * @Description 点播服务控制器
 */
@Slf4j
@RestController
@RequestMapping("/demand")
public class DemandBroadcastController {

    @Autowired
    private UserRecordService userRecordService;

    @Autowired
    private DemandBroadcastService demandBroadcastService;

    /**
     * 点播列表查询
     *
     * @param data：点播列表查询参数（type：类型（1-点播列表，2-我的收藏），pageNo：页码，pageSize：每页条数，searchContext：查询关键字）
     * @return DgovResponse
     */
    @PostMapping("/list")
    public DgovResponse list(HttpServletRequest request, @RequestBody JSONObject data) {
        try {
            // 点播列表类型校验
            String type = data.getString("type");
            if (!StrUtil.equalsAny(type, "1", "2")) {
                return DgovResponse.fail("请检查点播列表类型");
            }
            // 获取当前登录用户ID
            String userId = userRecordService.getUserRecord(request).getUserId();
            data.put("userId", userId);
            // 获取分页条件
            int pageNo = data.getIntValue("pageNo");
            if (0 == pageNo) {
                pageNo = 1;
            }
            int pageSize = data.getIntValue("pageSize");
            if (0 == pageSize) {
                pageSize = 10;
            }

            Page<DemandBroadcastVO> page = new Page<>(pageNo, pageSize);
            Page<DemandBroadcastVO> pageList;
            // 点播
            if ("1".equals(type)) {
                // 点播列表
                pageList = this.demandBroadcastService.queryList(page, data);
            } else {
                // 我的收藏
                pageList = this.demandBroadcastService.queryCollectList(page, data);
            }
            return DgovResponse.success(pageList);
        } catch (Exception e) {
            log.error("/demand/list error:" + e.getMessage());
            return DgovResponse.fail("点播列表查询失败");
        }
    }

    /**
     * 获取点播信息
     * @param demandId 点播详情
     * @return 结果
     */
    @GetMapping("/info/{id}")
    public DgovResponse getDemandBroadcast(@PathVariable("id") String demandId){
        try {
            DemandBroadcast demandBroadcast = demandBroadcastService.getById(demandId);
            return DgovResponse.success(demandBroadcast);
        }catch (Exception exception){
            log.error("get demand info failed!",exception);
            return DgovResponse.fail("获取点播信息失败");
        }
    }

    /**
     * 获取点播url地址列表
     * @param demandId 点播id
     * @return 点播url列表
     */
    @GetMapping("/videourl/{id}")
    public DgovResponse getDemandBroadcastUrls(@PathVariable("id") String demandId){
        try {
            List<DemandVideoUrl> videoUrls = demandBroadcastService.
                    getDemandBroadcastVideoUrl(demandId);
            return DgovResponse.success(videoUrls);
        }catch (Exception exception){
            log.error("get demand info failed!",exception);
            return DgovResponse.fail("获取点播播放地址url失败");
        }
    }

}
